// 攻击200（像素）范围内的敌人兵，每秒攻击5次，1次击杀1个兵。

// let Camp = require("Camp");

cc.Class({
    extends: cc.Component,

    properties: 
    {
        _ControlNode: null,
        ControlNode:
        {
            get: function()
            {
                return this._ControlNode;
            },
            set: function(val)
            {
                this._ControlNode = val;
            }
        },
        _AttackInterval: 1,  // 攻击间隔
        AttackInterval:
        {
            get: function()
            {
                return this._AttackInterval;
            },
            set: function(val)
            {
                this._AttackInterval = val;
            },
            visible: false,
        },

        _AttackTime: 0,   // 攻击时间
        AttackTime:
        {
            get: function()
            {
                return this._AttackTime;
            },
            set: function(val)
            {
                this._AttackTime = val;
            },
            visible: false,
        },

        // _Type: 3,   // 1我方，2敌方，3中立
        // Type:
        // {
        //     get: function()
        //     {
        //         return this._Type;
        //     },
        //     set: function(val)
        //     {
        //         this._Type = val;
        //     }
        // },

        _TargetList:
        {
            default: [],
            type: [cc.Node],
        }
    },

    onLoad: function() 
    {
        this.ControlNode = cc.find("Canvas/ControlNode");
        this.controlScript = this.ControlNode.getComponent("MyControl");
        this.AttackInterval = 0.2;
    },

    start: function()
    {
        
    },

    onCollisionEnter: function(other, self)
    {
        if(self.tag === 1)
        {
            let campScript = self.node.getComponent("Camp");
            let soilderScript = other.node.getComponent("Soilder");
            if(soilderScript.Type !== campScript.Type)  // 不同阵营的才进行攻击
            {   
                this.controlScript.addTowerTarget(other.node);
            }
        }
    },

    onCollisionExit: function(other, self)
    {
        if(self.tag === 1)
        {
            this.controlScript.removeTowerTarget(other);
        }
    },

    start: function() 
    {

    },

    update: function(dt) 
    {
        if(this.controlScript.getTowerTargetLength() > 0)
        {
            if (this.AttackTime > this.AttackInterval) 
            {
                console.log("tower attack", this.AttackTime);
                this.controlScript.attackTarget();
                this.AttackTime = 0;
            }
            this.AttackTime = this.AttackTime+dt;
        }
    },
});
